package org.zoomdev.zoom.dao.utils;

import org.zoomdev.zoom.common.caster.Caster;
import org.zoomdev.zoom.common.caster.ValueCaster;
import org.zoomdev.zoom.common.utils.Classes;
import org.zoomdev.zoom.common.utils.Page;

import java.lang.reflect.Field;

public final class PageColumnInfo {

    public static final PageColumnInfo count = new PageColumnInfo();

    static {
        count.selectRaw = DaoUtils.SELECT_COUNT;
        count.setField(Classes.getField(Page.class, "totalRows"));
    }

    private String selectRaw;


    private ValueCaster caster;

    private Field field;


    public void setField(Field field) {
        this.field = field;
        field.setAccessible(true);
        this.caster = Caster.wrap(field.getType());
    }

    public String getSelectRaw() {
        return selectRaw;
    }

    public void setSelectRaw(String selectRaw) {
        this.selectRaw = selectRaw;
    }


    public void setValue(Object obj, Object value) {
        if (value == null) return;
        Classes.set(field, obj, caster.to(value));
    }
}
